<%@page import="cn.sendto.dbhelper.DBHelper"%>
<%@page import="cn.sendto.model.StudentEntity"%>
<%@page import="java.util.List"%>
<%@page import="cn.sendto.mapper.StudentMapper"%>
<%@page import="cn.sendto.mapper.IRowMapper"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	//第一步：设置字符编码格式
	request.setCharacterEncoding("utf-8");
	//第二步：获取表单数据
	String username = request.getParameter("username");
	
	String password = request.getParameter("password");
	//第三步：进行数据的非空验证
	if(username==null||username.equals("")){  
		request.setAttribute("message", "请输入用户名");
		request.getRequestDispatcher("login.jsp").forward(request, response);
	}else if(password==null||password.equals("")){
		request.setAttribute("message", "请输入密码");
		request.getRequestDispatcher("login.jsp").forward(request, response);
	}else{
		//第四步：数据库查询数据
		DBHelper helper = new DBHelper();
		
		IRowMapper mapper = new StudentMapper();
		
		String sql ="select * from studentinfo where studentname=?";
		
		List <StudentEntity> list =  helper.executeQuery(sql, mapper, username);
		
		helper.close();
		if(list.size()==0){
			request.setAttribute("message", "用户名不存在");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}else{
			if(password.equals(list.get(0).getPassword())){
				request.getSession().setAttribute("username", username);
				response.sendRedirect("main.jsp");
				//request.getRequestDispatcher("main.jsp").forward(request, response);
			}else{
				request.setAttribute("message", "密码不正确");
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}
		}
	}
	//第三步：用户名和密码的判断
//	if(username.equals("admin")&&password.equals("admin")){
//		//请求转发
//		request.getSession().setAttribute("username", username);
//		request.getRequestDispatcher("main.jsp").forward(request, response);
//	}else if(username.equals("user")&&password.equals("pass")){
//		//重定向
//		request.setAttribute("username", username);
//		response.sendRedirect("top.jsp");
//	}
%>